<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta name="generator" content="Hugo 0.82.1" />
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[golang] 变量声明和初始化 var, :=, new() 和 make()#说明#go 语言中，提供了多种变量声明和初始化的方法。这里着重一一说明。并提供一个简单的指南。
初始化的方式# 变量声明: var a = false 零值初始化: var a int32 短变量声明: := make() new()  指南# 使用make()，来初始化slice，map 和channel 。 大多数场合，类型明确的场合下，使用短变量声明方式:=。 当使用文字方式初始化一个变量，并且需要指明类型时，使用var变量声明方式。 避免使用new()，除非你需要一个指针变量。  变量声明方式#go 语言可以使用 var 来声明一个变量，并指明变量的数据类型。
// 初始化整数变量，值为10。 	var v int = 10 fmt.Println(v) // 输出: 10  // 变量声明: 一个slice变量 	var vSlice []int = []int{1, 2, 3, 4} fmt.">
<meta name="theme-color" content="#FFFFFF"><meta property="og:title" content="" />
<meta property="og:description" content="[golang] 变量声明和初始化 var, :=, new() 和 make()#说明#go 语言中，提供了多种变量声明和初始化的方法。这里着重一一说明。并提供一个简单的指南。
初始化的方式# 变量声明: var a = false 零值初始化: var a int32 短变量声明: := make() new()  指南# 使用make()，来初始化slice，map 和channel 。 大多数场合，类型明确的场合下，使用短变量声明方式:=。 当使用文字方式初始化一个变量，并且需要指明类型时，使用var变量声明方式。 避免使用new()，除非你需要一个指针变量。  变量声明方式#go 语言可以使用 var 来声明一个变量，并指明变量的数据类型。
// 初始化整数变量，值为10。 	var v int = 10 fmt.Println(v) // 输出: 10  // 变量声明: 一个slice变量 	var vSlice []int = []int{1, 2, 3, 4} fmt." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/docs/data-init/" /><meta property="article:section" content="docs" />



<title>Data Init | My Golang Book</title>
<link rel="manifest" href="/manifest.json">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/book.min.2bc2364a4a4b31f0ec1debecf0a8f90a840821f143dfe1ac6a0f7cbcbdcf64ac.css" integrity="sha256-K8I2SkpLMfDsHevs8Kj5CoQIIfFD3&#43;Gsag98vL3PZKw=">
<script defer src="/en.search.min.84ce9693e6c02d95376f29aeb794c11824d09ed0440f910f008e91e9ed0436e4.js" integrity="sha256-hM6Wk&#43;bALZU3bymut5TBGCTQntBED5EPAI6R6e0ENuQ="></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->

  
</head>

<body dir="ltr">
  <input type="checkbox" class="hidden toggle" id="menu-control" />
  <input type="checkbox" class="hidden toggle" id="toc-control" />
  <main class="container flex">
    <aside class="book-menu">
      <div class="book-menu-content">
        
  <nav>
<h2 class="book-brand">
  <a href="/"><span>My Golang Book</span>
  </a>
</h2>


<div class="book-search">
  <input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
  <div class="book-search-spinner hidden"></div>
  <ul id="book-search-results"></ul>
</div>











  



  
  <ul>
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/introduction/" class="">Golang 介绍</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/installation/" class="">安装、准备开发环境</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/go-routine/" class="">并发处理 - Go 协程</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/workspace-module/" class="">管理Go模块</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/erro-handling/" class="">错误处理</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/resources/" class="">Golang 资源</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/statements/" class="">Go For语句</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/guidelines/" class="">Go 开发规范</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/logging/" class="">日志</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-init/" class=" active">Data Init</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/data-types/" class="">Data Types</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/debug/" class="">Debug</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/dynamic-programing/" class="">Dynamic Programing</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/ecosystem/" class="">Ecosystem</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/performance/" class="">Performance</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-interface/" class="">Struct Interface</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="http://example.org/docs/struct-tags/" class="">Struct Tags</a>
  

        </li>
      
    
  </ul>















</nav>




  <script>(function(){var a=document.querySelector("aside.book-menu nav");addEventListener("beforeunload",function(b){localStorage.setItem("menu.scrollTop",a.scrollTop)}),a.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>


 
      </div>
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>Data Init</strong>

  <label for="toc-control">
    
    <img src="/svg/toc.svg" class="book-icon" alt="Table of Contents" />
    
  </label>
</div>


  
  <aside class="hidden clearfix">
    
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#说明">说明</a></li>
    <li><a href="#初始化的方式">初始化的方式</a></li>
    <li><a href="#指南">指南</a></li>
    <li><a href="#变量声明方式">变量声明方式</a></li>
    <li><a href="#短变量声明方式">短变量声明方式</a></li>
    <li><a href="#newt">new(T)</a></li>
    <li><a href="#make">make()</a></li>
    <li><a href="#完整源码">完整源码</a></li>
  </ul>
</nav>



  </aside>
  
 
      </header>

      
      
  <article class="markdown"><h1 id="golang-变量声明和初始化-var--new-和-make">
  [golang] 变量声明和初始化 var, :=, new() 和 make()
  <a class="anchor" href="#golang-%e5%8f%98%e9%87%8f%e5%a3%b0%e6%98%8e%e5%92%8c%e5%88%9d%e5%a7%8b%e5%8c%96-var--new-%e5%92%8c-make">#</a>
</h1>
<h2 id="说明">
  说明
  <a class="anchor" href="#%e8%af%b4%e6%98%8e">#</a>
</h2>
<p>go 语言中，提供了多种变量声明和初始化的方法。这里着重一一说明。并提供一个简单的指南。</p>
<h2 id="初始化的方式">
  初始化的方式
  <a class="anchor" href="#%e5%88%9d%e5%a7%8b%e5%8c%96%e7%9a%84%e6%96%b9%e5%bc%8f">#</a>
</h2>
<ul>
<li>变量声明: <code>var a = false</code></li>
<li>零值初始化: <code>var a int32</code></li>
<li>短变量声明: <code>:=</code></li>
<li>make()</li>
<li>new()</li>
</ul>
<h2 id="指南">
  指南
  <a class="anchor" href="#%e6%8c%87%e5%8d%97">#</a>
</h2>
<ul>
<li>使用<code>make()</code>，来初始化<code>slice</code>，<code>map</code> 和<code>channel</code> 。</li>
<li>大多数场合，类型明确的场合下，使用<strong>短变量声明</strong>方式<code>:=</code>。</li>
<li>当使用文字方式初始化一个变量，并且需要指明类型时，使用<code>var</code><strong>变量声明</strong>方式。</li>
<li>避免使用<code>new()</code>，除非你需要一个指针变量。</li>
</ul>
<h2 id="变量声明方式">
  变量声明方式
  <a class="anchor" href="#%e5%8f%98%e9%87%8f%e5%a3%b0%e6%98%8e%e6%96%b9%e5%bc%8f">#</a>
</h2>
<p>go 语言可以使用 <code>var</code> 来声明一个变量，并指明变量的数据类型。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">	<span style="color:#75715e">// 初始化整数变量，值为10。
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">v</span> <span style="color:#66d9ef">int</span> = <span style="color:#ae81ff">10</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">v</span>)
	<span style="color:#75715e">// 输出: 10
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 变量声明: 一个slice变量
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">vSlice</span> []<span style="color:#66d9ef">int</span> = []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">vSlice</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">vSlice</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [1 2 3 4] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个map变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">vMap</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>{
		<span style="color:#e6db74">&#34;a&#34;</span>: <span style="color:#ae81ff">1</span>,
		<span style="color:#e6db74">&#34;b&#34;</span>: <span style="color:#ae81ff">2</span>,
	}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">vMap</span>)
	<span style="color:#75715e">// 输出: map[a:1 b:2]
</span></code></pre></div><h2 id="短变量声明方式">
  短变量声明方式
  <a class="anchor" href="#%e7%9f%ad%e5%8f%98%e9%87%8f%e5%a3%b0%e6%98%8e%e6%96%b9%e5%bc%8f">#</a>
</h2>
<p>short variable declarations 符号: <code>:=</code>。</p>
<p>短变量声明时，变量的默认类型是: <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</code>, <code>complex128</code> or <code>string</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">	<span style="color:#75715e">// 短变量声明: 一个整数变量。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvInt</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">10</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvInt</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">sdvInt</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: 10 type:  int
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个slice变量
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvSlice</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvSlice</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">sdvSlice</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [1 2 3 4] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个map变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvMap</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>{
		<span style="color:#e6db74">&#34;a&#34;</span>: <span style="color:#ae81ff">1</span>,
		<span style="color:#e6db74">&#34;b&#34;</span>: <span style="color:#ae81ff">2</span>,
	}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvMap</span>)
	<span style="color:#75715e">// 输出: map[a:1 b:2]
</span><span style="color:#75715e"></span>
</code></pre></div><h2 id="newt">
  new(T)
  <a class="anchor" href="#newt">#</a>
</h2>
<p><code>new(T)</code>的特点：</p>
<ul>
<li>根据类型 T 分配内存</li>
<li>设置内存为 0</li>
<li>返回内存的指针</li>
</ul>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">	<span style="color:#75715e">// 初始化一个整数指针变量，指向的值为0
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">i3</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">i3</span>)

	<span style="color:#75715e">// 初始化一个slice指针变量
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">i4</span> = new([<span style="color:#ae81ff">10</span>]<span style="color:#66d9ef">int</span>)[<span style="color:#ae81ff">0</span>:<span style="color:#ae81ff">5</span>]
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">i4</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">i4</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [0 0 0 0 0] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 初始化一个map指针变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">i5</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">i5</span>)
	<span style="color:#75715e">// 输出: map[]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 初始化一个chan指针变量，指向的值为nil
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">i6</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">i6</span>)
	<span style="color:#75715e">// 输出: nil
</span></code></pre></div><h2 id="make">
  make()
  <a class="anchor" href="#make">#</a>
</h2>
<p>make 只用于初始化 <code>slice</code>，<code>map</code> 和 <code>channel</code>。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">	<span style="color:#75715e">// make只能用于创建slice, map, channel
</span><span style="color:#75715e"></span>	<span style="color:#75715e">// 切片类型(slice)
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">makeSlice</span> <span style="color:#f92672">:=</span> make([]<span style="color:#66d9ef">int</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">10</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeSlice</span>)
	<span style="color:#75715e">// 输出: [0 0 0 0 0]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// Map 类型
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">makeMap</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeMap</span>)
	<span style="color:#75715e">// 输出: map[]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// Channel 类型
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">makeChan</span> <span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int32</span> = make(<span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int32</span>, <span style="color:#ae81ff">100</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeChan</span>)
	<span style="color:#75715e">// 输出: 0xc000112000
</span></code></pre></div><h2 id="完整源码">
  完整源码
  <a class="anchor" href="#%e5%ae%8c%e6%95%b4%e6%ba%90%e7%a0%81">#</a>
</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

<span style="color:#f92672">import</span> (
	<span style="color:#e6db74">&#34;fmt&#34;</span>
	<span style="color:#e6db74">&#34;reflect&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {

	<span style="color:#75715e">// 初始化整数变量，值为10。
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">v</span> <span style="color:#66d9ef">int</span> = <span style="color:#ae81ff">10</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">v</span>)
	<span style="color:#75715e">// 输出: 10
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 变量声明: 一个slice变量
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">vSlice</span> []<span style="color:#66d9ef">int</span> = []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">vSlice</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">vSlice</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [1 2 3 4] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个map变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">vMap</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>{
		<span style="color:#e6db74">&#34;a&#34;</span>: <span style="color:#ae81ff">1</span>,
		<span style="color:#e6db74">&#34;b&#34;</span>: <span style="color:#ae81ff">2</span>,
	}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">vMap</span>)
	<span style="color:#75715e">// 输出: map[a:1 b:2]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个整数变量。
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvInt</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">10</span>
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvInt</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">sdvInt</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: 10 type:  int
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个slice变量
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvSlice</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">int</span>{<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvSlice</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">sdvSlice</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [1 2 3 4] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 短变量声明: 一个map变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">sdvMap</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>{
		<span style="color:#e6db74">&#34;a&#34;</span>: <span style="color:#ae81ff">1</span>,
		<span style="color:#e6db74">&#34;b&#34;</span>: <span style="color:#ae81ff">2</span>,
	}
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">sdvMap</span>)
	<span style="color:#75715e">// 输出: map[a:1 b:2]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 初始化一个整数指针变量，指向的值为0
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">newInt</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">newInt</span>)

	<span style="color:#75715e">// 初始化一个slice指针变量
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">newSlice</span> = new([<span style="color:#ae81ff">10</span>]<span style="color:#66d9ef">int</span>)[<span style="color:#ae81ff">0</span>:<span style="color:#ae81ff">5</span>]
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">newSlice</span>, <span style="color:#e6db74">&#34;type: &#34;</span>, <span style="color:#a6e22e">reflect</span>.<span style="color:#a6e22e">TypeOf</span>(<span style="color:#a6e22e">newSlice</span>).<span style="color:#a6e22e">Kind</span>())
	<span style="color:#75715e">// 输出: [0 0 0 0 0] type: slice
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 初始化一个map指针变量，指向的值为[]
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">newMap</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">newMap</span>)
	<span style="color:#75715e">// 输出: map[]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// 初始化一个chan指针变量，指向的值为nil
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">newChan</span> <span style="color:#f92672">*</span><span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int</span> = new(<span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#f92672">*</span><span style="color:#a6e22e">newChan</span>)
	<span style="color:#75715e">// 输出: nil
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// make只能用于创建slice, map, channel
</span><span style="color:#75715e"></span>	<span style="color:#75715e">// 切片类型(slice)
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">makeSlice</span> <span style="color:#f92672">:=</span> make([]<span style="color:#66d9ef">int</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">10</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeSlice</span>)
	<span style="color:#75715e">// 输出: [0 0 0 0 0]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// Map 类型
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">makeMap</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span> = make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">int</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeMap</span>)
	<span style="color:#75715e">// 输出: map[]
</span><span style="color:#75715e"></span>
	<span style="color:#75715e">// Channel 类型
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">makeChan</span> <span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int32</span> = make(<span style="color:#66d9ef">chan</span> <span style="color:#66d9ef">int32</span>, <span style="color:#ae81ff">100</span>)
	<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">makeChan</span>)
	<span style="color:#75715e">// 输出: 0xc000112000
</span><span style="color:#75715e"></span>
}

</code></pre></div></article>
 
      

      <footer class="book-footer">
        
  <div class="flex flex-wrap justify-between">





</div>

 
        
      </footer>

      
  
  <div class="book-comments">

</div>
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      <div class="book-toc-content">
        
  
<nav id="TableOfContents">
  <ul>
    <li><a href="#说明">说明</a></li>
    <li><a href="#初始化的方式">初始化的方式</a></li>
    <li><a href="#指南">指南</a></li>
    <li><a href="#变量声明方式">变量声明方式</a></li>
    <li><a href="#短变量声明方式">短变量声明方式</a></li>
    <li><a href="#newt">new(T)</a></li>
    <li><a href="#make">make()</a></li>
    <li><a href="#完整源码">完整源码</a></li>
  </ul>
</nav>


 
      </div>
    </aside>
    
  </main>

  
</body>

</html>












